﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Course1
{
    /// <summary>
    /// 异常及处理
    ///     了解常见的异常，以及触发机制
    ///     了解异常处理机制和规则
    ///     应该尽量避免异常的产生，因为生成一个异常很耗时
    /// </summary>
    internal class Example8
    {
        public void DoSome(int? age)
        {
            if (age == null)
            {
                //抛出异常
                throw new ArgumentNullException(nameof(age));
            }
        }
        /// <summary>
        /// 目前不想实现
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public int DoSome()
        {
            throw new NotImplementedException();
        }

        public void Test1()
        {
            try
            {
                DoSome(null);
            }
            catch (ArgumentNullException ex)
            {
                DoSome(null);
            }
            catch (Exception ex)
            {
                throw;//继续上抛出
            }
            finally
            {
                Console.WriteLine("出现了异常");
            }
        }
        public double ReadNumber()
        {
            double resutl = 0;
            var text = Console.ReadLine();
            try
            {
                return Convert.ToInt64(text);
            }
            catch (Exception ex)
            {
                //return或者throw执行一个就行了
                throw new InputException(text, "不是合法的数字", ex);
            }
        }
    }

    public class InputException : Exception
    {
        public string Input { get; }

        public InputException(string input, string message, Exception exception)
            : base(message, exception)
        {
            Input = input;
        }
    }
}
